/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.DiagnosticoController;
import Model.Diagnostico;
import javax.swing.JOptionPane;

/**
 *
 * @author Tiago
 */
public class DiagnosticoView extends javax.swing.JDialog {
    //variável para diferenciar alteração de inclusão. Inclusão = False, Alteração = True
    boolean alterar;
    int codigo;
    DiagnosticoController dControl = new DiagnosticoController();
    
    public DiagnosticoView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo( null );
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnDiagnosticoCampo = new javax.swing.JPanel();
        tfDiagnosticoNome = new javax.swing.JTextField();
        lbDiagnosticoNome = new javax.swing.JLabel();
        lbDiagnosticoCaracteristica = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taDiagnosticoCaracteristica = new javax.swing.JTextArea();
        pnDiagnosticoBotao = new javax.swing.JPanel();
        btDiagnosticoSalvar = new javax.swing.JButton();
        btDiagnosticoCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Diagnóstico");

        pnDiagnosticoCampo.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados de Diagnóstico"));

        tfDiagnosticoNome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfDiagnosticoNomeKeyReleased(evt);
            }
        });

        lbDiagnosticoNome.setText("Nome:");

        lbDiagnosticoCaracteristica.setText("Característica:");

        taDiagnosticoCaracteristica.setColumns(20);
        taDiagnosticoCaracteristica.setRows(5);
        taDiagnosticoCaracteristica.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                taDiagnosticoCaracteristicaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(taDiagnosticoCaracteristica);

        javax.swing.GroupLayout pnDiagnosticoCampoLayout = new javax.swing.GroupLayout(pnDiagnosticoCampo);
        pnDiagnosticoCampo.setLayout(pnDiagnosticoCampoLayout);
        pnDiagnosticoCampoLayout.setHorizontalGroup(
            pnDiagnosticoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDiagnosticoCampoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnDiagnosticoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lbDiagnosticoNome, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbDiagnosticoCaracteristica, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnDiagnosticoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tfDiagnosticoNome, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnDiagnosticoCampoLayout.setVerticalGroup(
            pnDiagnosticoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDiagnosticoCampoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnDiagnosticoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfDiagnosticoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbDiagnosticoNome))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnDiagnosticoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnDiagnosticoCampoLayout.createSequentialGroup()
                        .addComponent(lbDiagnosticoCaracteristica)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
                .addContainerGap())
        );

        pnDiagnosticoBotao.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        btDiagnosticoSalvar.setText("Salvar");
        btDiagnosticoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDiagnosticoSalvarActionPerformed(evt);
            }
        });

        btDiagnosticoCancelar.setText("Cancelar");
        btDiagnosticoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDiagnosticoCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnDiagnosticoBotaoLayout = new javax.swing.GroupLayout(pnDiagnosticoBotao);
        pnDiagnosticoBotao.setLayout(pnDiagnosticoBotaoLayout);
        pnDiagnosticoBotaoLayout.setHorizontalGroup(
            pnDiagnosticoBotaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDiagnosticoBotaoLayout.createSequentialGroup()
                .addGap(96, 96, 96)
                .addComponent(btDiagnosticoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btDiagnosticoCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(94, Short.MAX_VALUE))
        );
        pnDiagnosticoBotaoLayout.setVerticalGroup(
            pnDiagnosticoBotaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDiagnosticoBotaoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnDiagnosticoBotaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btDiagnosticoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btDiagnosticoCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnDiagnosticoCampo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnDiagnosticoBotao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnDiagnosticoCampo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnDiagnosticoBotao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void receberDadosDiagnostico(Diagnostico d){
        tfDiagnosticoNome.setText(d.getNome());
        taDiagnosticoCaracteristica.setText(d.getCaracteristica());
    }
    
    private void btDiagnosticoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDiagnosticoCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btDiagnosticoCancelarActionPerformed

    private void btDiagnosticoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDiagnosticoSalvarActionPerformed
        if(tfDiagnosticoNome.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Preencha os campos corretamente!");
        }
        else{
            Diagnostico d = new Diagnostico();
            d.setNome(tfDiagnosticoNome.getText());
            d.setCaracteristica(taDiagnosticoCaracteristica.getText());
            if(alterar == false){
                dControl.incluirDiagnostico(d);
                tfDiagnosticoNome.setText("");
                taDiagnosticoCaracteristica.setText("");
                tfDiagnosticoNome.requestFocus();
            }
            else{
                d.setCodigo(codigo);
                dControl.alterarDiagnostico(d);
            }
        }
        
    }//GEN-LAST:event_btDiagnosticoSalvarActionPerformed

    private void tfDiagnosticoNomeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfDiagnosticoNomeKeyReleased
        tfDiagnosticoNome.setText(tfDiagnosticoNome.getText().toUpperCase());
    }//GEN-LAST:event_tfDiagnosticoNomeKeyReleased

    private void taDiagnosticoCaracteristicaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taDiagnosticoCaracteristicaKeyReleased
        taDiagnosticoCaracteristica.setText(taDiagnosticoCaracteristica.getText().toUpperCase());
    }//GEN-LAST:event_taDiagnosticoCaracteristicaKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DiagnosticoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DiagnosticoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DiagnosticoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DiagnosticoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DiagnosticoView dialog = new DiagnosticoView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btDiagnosticoCancelar;
    private javax.swing.JButton btDiagnosticoSalvar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbDiagnosticoCaracteristica;
    private javax.swing.JLabel lbDiagnosticoNome;
    private javax.swing.JPanel pnDiagnosticoBotao;
    private javax.swing.JPanel pnDiagnosticoCampo;
    private javax.swing.JTextArea taDiagnosticoCaracteristica;
    private javax.swing.JTextField tfDiagnosticoNome;
    // End of variables declaration//GEN-END:variables
}
